package com.caifeng.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

public class AnimalAdapter extends BaseAdapter {

    private List<Animal> mData;
    private Context mContext;

    public AnimalAdapter(List<Animal> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder;

    public class ViewHolder {
        private ImageView pic;
        private TextView name, speak;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal, parent, false);

            viewHolder.name = convertView.findViewById(R.id.txt_aName);
            viewHolder.speak = convertView.findViewById(R.id.txt_aSpeak);
            viewHolder.pic = convertView.findViewById(R.id.img_icon);
            convertView.setTag(viewHolder);
            //            ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
//            TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
//            TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);
//            img_icon.setBackgroundResource(mData.get(position).getaIcon());
//            txt_aName.setText(mData.get(position).getaName());
//            txt_aSpeak.setText(mData.get(position).getaSpeak());
//            return convertView;
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(mData.get(position).getaName());
        viewHolder.pic.setImageResource(mData.get(position).getaIcon());
        viewHolder.speak.setText(mData.get(position).getaSpeak());
        return convertView;
    }
}